package com.mx.restful.view.window;

import com.mx.restful.beans.Request;
import com.mx.restful.view.icon.Icons;
//import com.github.javaparser.JavaParser;
import com.intellij.ui.ColoredTreeCellRenderer;
import com.intellij.ui.SimpleTextAttributes;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 * @Author: mengxiang.
 * @Date: 2020/6/22.
 * @Time: 11:37 上午.
 * @Description:
 */
public class RestfulTreeCellRenderer extends ColoredTreeCellRenderer {

//    private static JavaParser javaParser = new JavaParser();

    @Override
    public void customizeCellRenderer(
            @NotNull JTree tree, Object value,
            boolean selected,
            boolean expanded,
            boolean leaf,
            int row, boolean hasFocus) {
        Object obj = ((DefaultMutableTreeNode) value).getUserObject();
        if (obj instanceof Request) {
            Request node = (Request) obj;

            setMethodTypeAndPath(node, selected);
        } else if (obj instanceof String) {
            append((String) obj, SimpleTextAttributes.GRAYED_BOLD_ATTRIBUTES);
        } else if (obj instanceof Integer) {
            setIcon(Icons.SERVICE);
            append(String.format("找到 %s 个接口", obj));
        }
    }

    private void setMethodTypeAndPath(@Nullable Request node, boolean selected) {
        if (node == null) {
            return;
        }
        if (selected) {
            setIcon(node.getSelectIcon());
        } else {
            setIcon(node.getIcon());
        }
        append(node.getPath());
        SimpleTextAttributes simpleTextAttributes = SimpleTextAttributes.DARK_TEXT;
        if (StringUtils.isNotBlank(node.getComment())) {
            append("\t\t[" + node.getComment() + "]", simpleTextAttributes);
        }

    }
}
